<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 * @property int $appid 
 * @property string $account
 * @property string $username
 * @property string $avatar
 * @property int $fd
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 */
class UserBind extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_bind';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['appid' => 'integer', 'fd' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', 'last_ack_msgid' => 'integer'];

    /**
     * 登录(绑定) 修改用户信息
     * @param $appid
     * @param $array
     * @return array|string
     */
    public static function updateUser($appid, $array)
    {
        $account  = $array['account'] ?? '';
        $fd       = isset($array['fd']) ? intval($array['fd']) : 0;
        $username = $array['username'] ?? '';
        $avatar   = $array['avatar'] ?? '';

        if (empty($account))
            return 'account is null';

        $where = [
            'account' => $account,
            'appid'   => $appid
        ];

        $update_data = [];
        if ($fd > 0)
            $update_data['fd']       = $fd;
        if ($username)
            $update_data['username'] = $username;
        if ($avatar)
            $update_data['avatar']   = $avatar;

        if (self::updateOrInsert($where, $update_data))
        {
            $update_data['account'] = $account;
            return $update_data;
        }
        return '登录失败，请联系管理员';
    }

    /**
     * 修改用户信息
     * @param $appid
     * @param $array
     * @return array|string
     */
    public static function updateFriendUser($appid, $array)
    {
        $account  = $array['account'] ?? '';
        $username = $array['username'] ?? '';
        $avatar   = $array['avatar'] ?? '';

        if (empty($account))
        {
            return 'account is null';
        }

        $where = ['appid'=>$appid,'account'=>$account];

        $user = self::where($where)->first(['account','username','avatar']);
        if (!$user)
            return "用户未登录 $account Im系统";

        $update = [];

        if (!empty($username))
            $update['username'] = $username;

        if(!empty($avatar))
            $update['avatar'] = $avatar;

        if (empty($update))
        {
            return '昵称 和 头像 必填一个';
        }

        if (self::where($where)->update($update) > 0)
        {
            return [
                'new' => $update,
                'old' => $user
            ];
        }
        return '修改失败';
    }

    public function getGlobaMsg($appid, $account)
    {
        $private = Msg::where(['appid'=>$appid,'account'=>$account,'status'=>2])->first(['id']);

    }

}